<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="账单描述" prop="billDesc">
        <el-input
          v-model="queryParams.billDesc"
          placeholder="请输入账单描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账单类型" prop="billFlag">
        <el-input
          v-model="queryParams.billFlag"
          placeholder="请输入账单类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="缴费类型" prop="expTypeName">
        <el-input
          v-model="queryParams.expTypeName"
          placeholder="请输入缴费类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option
            v-for="item in shList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="缴费时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="adminRoles === 'school'">
        <el-button
          type="primary"
          size="mini"
          @click="handleImport"
          v-hasPermi="['business:bill:export']"
          >导入</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="billList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="学校名称" align="center" prop="schoolName" />
      <el-table-column label="账单描述" align="center" prop="billDesc" />
      <el-table-column label="缴费类型" align="center" prop="expTypeName" />
      <el-table-column label="合计金额" align="center" prop="totalAmt" />
      <el-table-column label="缴费金额" align="center" prop="paidAmt" />
      <el-table-column label="未缴金额" align="center" prop="unpaidAmt" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 2">缴费完成</span>
          <span v-if="scope.row.status == 1">部分缴费</span>
          <span v-if="scope.row.status == 0">未缴费</span>
        </template>
      </el-table-column>
      <el-table-column
        label="上传时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="缴费开始时间"
        align="center"
        prop="billPayStartDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billPayStartDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="缴费结束时间"
        align="center"
        prop="billPayEndDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billPayEndDate) }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >详情</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:bill:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学校id" prop="schoolId">
          <el-input v-model="form.schoolId" placeholder="请输入学校id" />
        </el-form-item>
        <el-form-item label="账单描述" prop="billDesc">
          <el-input v-model="form.billDesc" placeholder="请输入账单描述" />
        </el-form-item>
        <el-form-item label="账单类型" prop="billFlag">
          <el-input v-model="form.billFlag" placeholder="请输入账单类型" />
        </el-form-item>
        <el-form-item label="账单uuid" prop="billId">
          <el-input v-model="form.billId" placeholder="请输入账单uuid" />
        </el-form-item>
        <el-form-item label="账单支付结束时间" prop="billPayEndDate">
          <el-date-picker
            clearable
            v-model="form.billPayEndDate"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择账单支付结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账单支付开始时间" prop="billPayStartDate">
          <el-date-picker
            clearable
            v-model="form.billPayStartDate"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择账单支付开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="billUpTime">
          <el-date-picker
            clearable
            v-model="form.billUpTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择${comment}"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="缴费类型名称" prop="expSubTypeName">
          <el-input
            v-model="form.expSubTypeName"
            placeholder="请输入缴费类型名称"
          />
        </el-form-item>
        <el-form-item label="缴费类型" prop="expTypeName">
          <el-input v-model="form.expTypeName" placeholder="请输入缴费类型" />
        </el-form-item>
        <el-form-item label="商户名称" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="商户编号" prop="merchantNo">
          <el-input v-model="form.merchantNo" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="已支付金额" prop="paidAmt">
          <el-input v-model="form.paidAmt" placeholder="请输入已支付金额" />
        </el-form-item>
        <el-form-item label="已支付次数" prop="paidCount">
          <el-input v-model="form.paidCount" placeholder="请输入已支付次数" />
        </el-form-item>
        <el-form-item
          label="已支付次数/总支付次数"
          prop="paidCountToTotalCount"
        >
          <el-input
            v-model="form.paidCountToTotalCount"
            placeholder="请输入已支付次数/总支付次数"
          />
        </el-form-item>
        <el-form-item label="支付金额" prop="payingAmt">
          <el-input v-model="form.payingAmt" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="支付次数" prop="payingCount">
          <el-input v-model="form.payingCount" placeholder="请输入支付次数" />
        </el-form-item>
        <el-form-item label="省份编码" prop="provinceNo">
          <el-input v-model="form.provinceNo" placeholder="请输入省份编码" />
        </el-form-item>
        <el-form-item label="推送标记" prop="pushFlag">
          <el-input v-model="form.pushFlag" placeholder="请输入推送标记" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmt">
          <el-input v-model="form.totalAmt" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="总次数" prop="totalCount">
          <el-input v-model="form.totalCount" placeholder="请输入总次数" />
        </el-form-item>
        <el-form-item label="未支付金额" prop="unpaidAmt">
          <el-input v-model="form.unpaidAmt" placeholder="请输入未支付金额" />
        </el-form-item>
        <el-form-item label="未支付次数" prop="unpaidCount">
          <el-input v-model="form.unpaidCount" placeholder="请输入未支付次数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-form ref="upform" :model="upform" :rules="uprules">
        <el-form-item label="账单描述" prop="billDesc">
          <el-input
            style="width: 240px"
            v-model="upform.billDesc"
            placeholder="请输入账单描述"
          />
        </el-form-item>
        <el-form-item label="缴费项目" prop="expTypeName">
          <el-input
            style="width: 240px"
            v-model="upform.expTypeName"
            placeholder="请输入缴费项目"
          />
        </el-form-item>
        <el-form-item label="缴费子项" prop="expSubTypeName">
          <el-input
            style="width: 240px"
            v-model="upform.expSubTypeName"
            placeholder="请输入缴费子项"
          />
        </el-form-item>
        <el-form-item label="缴费日期">
          <el-date-picker
            v-model="jfRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
      </el-form>

      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :data="upform"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBill,
  getBill,
  delBill,
  addBill,
  updateBill,
} from "@/api/business/bill";
import { getToken } from "@/utils/auth";

export default {
  name: "Bill",
  data() {
    return {
      shList: [
        { name: "未缴费", value: 0 },
        { name: "部分缴费", value: 1 },
        { name: "缴费完成", value: 2 },
      ],
      adminRoles: "",
      upform: {},
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/business/bill/importData",
      },
      // 日期范围
      dateRange: [],
      jfRange: [],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账单表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolId: null,
        billDesc: null,
        billFlag: null,
        billId: null,
        billPayEndDate: null,
        billPayStartDate: null,
        billPayType: null,
        billUpTime: null,
        expSubTypeName: null,
        expTypeName: null,
        merchantName: null,
        merchantNo: null,
        paidAmt: null,
        paidCount: null,
        paidCountToTotalCount: null,
        payingAmt: null,
        payingCount: null,
        provinceNo: null,
        pushFlag: null,
        totalAmt: null,
        totalCount: null,
        unpaidAmt: null,
        unpaidCount: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "id,自增不能为空", trigger: "blur" }],
        schoolId: [
          { required: true, message: "学校id不能为空", trigger: "blur" },
        ],
        billDesc: [
          { required: true, message: "账单描述不能为空", trigger: "blur" },
        ],
        billFlag: [
          { required: true, message: "账单类型不能为空", trigger: "blur" },
        ],
        billId: [
          { required: true, message: "账单uuid不能为空", trigger: "blur" },
        ],
        billPayEndDate: [
          {
            required: true,
            message: "账单支付结束时间不能为空",
            trigger: "blur",
          },
        ],
        billPayStartDate: [
          {
            required: true,
            message: "账单支付开始时间不能为空",
            trigger: "blur",
          },
        ],
        billPayType: [
          {
            required: true,
            message: "账单支付类型不能为空",
            trigger: "change",
          },
        ],
        billUpTime: [
          { required: true, message: "$comment不能为空", trigger: "blur" },
        ],
        expSubTypeName: [
          { required: true, message: "缴费类型名称不能为空", trigger: "blur" },
        ],
        expTypeName: [
          { required: true, message: "缴费类型不能为空", trigger: "blur" },
        ],
        merchantName: [
          { required: true, message: "商户名称不能为空", trigger: "blur" },
        ],
        merchantNo: [
          { required: true, message: "商户编号不能为空", trigger: "blur" },
        ],
        paidAmt: [
          { required: true, message: "已支付金额不能为空", trigger: "blur" },
        ],
        paidCount: [
          { required: true, message: "已支付次数不能为空", trigger: "blur" },
        ],
        paidCountToTotalCount: [
          {
            required: true,
            message: "已支付次数/总支付次数不能为空",
            trigger: "blur",
          },
        ],
        payingAmt: [
          { required: true, message: "支付金额不能为空", trigger: "blur" },
        ],
        payingCount: [
          { required: true, message: "支付次数不能为空", trigger: "blur" },
        ],
        provinceNo: [
          { required: true, message: "省份编码不能为空", trigger: "blur" },
        ],
        pushFlag: [
          { required: true, message: "推送标记不能为空", trigger: "blur" },
        ],
        totalAmt: [
          { required: true, message: "总金额不能为空", trigger: "blur" },
        ],
        totalCount: [
          { required: true, message: "总次数不能为空", trigger: "blur" },
        ],
        unpaidAmt: [
          { required: true, message: "未支付金额不能为空", trigger: "blur" },
        ],
        unpaidCount: [
          { required: true, message: "未支付次数不能为空", trigger: "blur" },
        ],
      },
      uprules: {
        billDesc: [
          { required: true, message: "账单描述不能为空", trigger: "blur" },
        ],
        expTypeName: [
          { required: true, message: "缴费项目不能为空", trigger: "blur" },
        ],
        expSubTypeName: [
          { required: true, message: "缴费子项不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    if (this.$store.state?.user?.roles) {
      this.adminRoles = this.$store.state?.user?.roles[0];
    }
    this.getList();
  },
  methods: {
    /** 查询账单列表 */
    getList() {
      this.loading = true;
      listBill(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.billList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    upRest() {
      this.upform = {
        billPayStartDate: null,
        billPayEndDate: null,
        billDesc: null,
        expTypeName: null,
        expSubTypeName: null,
      };
      this.jfRange = [];
      this.resetForm("upform");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        schoolId: null,
        billDesc: null,
        billFlag: null,
        billId: null,
        billPayEndDate: null,
        billPayStartDate: null,
        billPayType: null,
        billUpTime: null,
        expSubTypeName: null,
        expTypeName: null,
        merchantName: null,
        merchantNo: null,
        paidAmt: null,
        paidCount: null,
        paidCountToTotalCount: null,
        payingAmt: null,
        payingCount: null,
        provinceNo: null,
        pushFlag: null,
        totalAmt: null,
        totalCount: null,
        unpaidAmt: null,
        unpaidCount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: "/goods/detail",
        query: {
          id: row.id,
          item: JSON.stringify(row),
        },
      });
      // this.reset();
      // const id = row.id || this.ids;
      // getBill(id).then((response) => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改账单";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBill(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBill(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除账单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBill(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/bill/export",
        {
          ...this.queryParams,
        },
        `bill_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upRest();
      this.upload.title = "账单导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "business/bill/importTemplate",
        {},
        `账单管理_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      if (this.jfRange.length > 0) {
        this.upform.billPayStartDate = this.jfRange[0];
        this.upform.billPayEndDate = this.jfRange[1];
      } else {
        this.jfRange = [];
      }
      this.$refs["upform"].validate((valid) => {
        if (valid) {
          this.$refs.upload.submit();
        }
      });
    },
  },
};
</script>
